package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.reggie.bean.ShoppingCart;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.service.ShoppingCartService;
import com.zrrd.reggie.service.impl.ShoppingCartServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ThreadLocalUtil.getCurrentId());
        wrapper.orderByAsc("create_time");
        wrapper.ge("number",1);
        return R.success(shoppingCartService.list(wrapper));
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = shoppingCartService.addCart(shoppingCart);
        return R.success(cart);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentId());
        //删除
        shoppingCartService.remove(wrapper);
        return R.success("清除购物车成功");
    }

    //修改购物车中的菜品 或 套餐数量
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = shoppingCartService.sub(shoppingCart);
        return R.success(cart);
    }
}
